package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.bo.job.JobBO;
import com.ymk.core.code.cmd.JobCmdCode;
import com.ymk.core.code.req.ReqCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IJobService;
import com.ymk.service.QuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class JobPostCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        JobBO reqData = super.getData(sign, JobBO.class);

        if (ReqCode.IMMEDIATE_EXECUTION_JOB.equals(sign.getReqCode())){
            this.quartzService.immediateExecutionJob(reqData.getJobCode(),reqData.getJobGroup());
            return respSign;
        }
        reqData.setStatus(SysConstants.YES);
        super.setCreatedData(sign,reqData);
        this.JobService.post(reqData);
        this.quartzService.addJob(reqData.getJobCode(),reqData.getJobGroup(),reqData.getJobCode(),reqData.getJobGroup(),reqData.getCronExpression());

        return respSign;
    }

    @Override
    public String getCmdCode() {
        return JobCmdCode.JOB_POST;
    }

    @Autowired
    private IJobService JobService;
    @Autowired
    private QuartzService quartzService;
}